﻿var currentPageNum = 1;
var totalPageNum;
$(function(){
    $(".news1").fadeTo("fast", 0.8);
	loadComments(1);
	$("#pageFirst").click(function(){
		loadComments(1);
	});
	$("#pagePrev").click(function(){
		loadComments(currentPageNum - 1);
	});
	$("#pageNext").click(function(){
		loadComments(currentPageNum + 1);
	});
	$("#pageLast").click(function(){
		loadComments(totalPageNum);
	});	
});
function loadComments(pageNum){
	$.ajax({
		url:"./ashx/LoadComments.ashx",
		type:"POST",
		data:"pageNum=" + pageNum,
		success:function(r){
			r = nehnre.parseJSON(r);
			if(r.success){
				reloadData(r.result, pageNum);
				currentPageNum = pageNum;
				totalPageNum = r.totalPageNum;
				
				$("#sta").html("　　第{0}页，共{1}页".format(currentPageNum, totalPageNum));
				(function(){
					if($(document).scrollTop() > 0){
						$(document).scrollTop($(document).scrollTop() - $(document).scrollTop()/5);
						setTimeout(arguments.callee,1);
					}
				})();
			} else {
				if(r.code == 1){
					alert("已经是最后一页了！");
				}
				if(r.code == 2){
					alert("已经是第一页了！");
				}
			}
		}
	});
}

function reloadData(result, pageNum){
	$(".emptyClass").remove();
	var table = result.Table;
	var indexNum = (pageNum-1) * 50;
	var departmodel = $(".departmodel").clone();
	for(var i=0;i<table.length;i++){
		indexNum ++;
		var departName = table[i].DepartName;
		var coContactWay = table[i].co_contact_way;
		var coContent = table[i].co_content;
		var onerow = departmodel.clone();
		
		
		onerow.children().eq(1).html(indexNum);
		onerow.children().eq(3).html(departName);
		onerow.children().eq(5).html(coContactWay);
		onerow.children().eq(7).html(coContent);
		console.log(i);
		$(".departmodel").last().after(onerow);
	}	
	$(".departmodel").each(function(n){
		if(n > 0){
			$(this).removeClass("departmodel");
			$(this).addClass("emptyClass");
		}
	});
}